package com.baidu.io;

import java.io.*;

public class D04_FileCopy {
    public static void main(String[] args) {
        //变量定义在外边 用于final获取
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            //1.定义字节输入流
            inputStream = new FileInputStream("src/002.dat");

            //2.定义字节输出流
            outputStream = new FileOutputStream("src/002_copy.txt");

            //3.定义单次输出缓冲大小
            byte[] bytes = new byte[1024];

            //4.循环缓冲写入文件
            int byteCnt;
            //4.1.每次计算获取字节大小用于最后一桶的字节数据计数
            while ((byteCnt = inputStream.read(bytes)) != -1){
                //4.2.指定写出缓冲字节数组的角标 防止最后一桶字节超界
                outputStream.write(bytes, 0, byteCnt);
            }
            System.out.println("复制完成");


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("执行final了");

            //5.关闭资源 关闭资源前需要进行非空校验
            try {
                if(null != outputStream) outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(null != inputStream) inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
